import Vue from 'vue'
import { wxappAuth, getUserInfo, wxappBindingPhone, getUnhandledMsgCount} from '@/api/user'
import store from '@/store'
import cookie from '@/utils/store/cookie'
import stringify from '@/utils/querystring'
import { VUE_APP_API_URL } from '@/config'
import { wechat, auth, oAuth, toAuth } from '@/libs/wechat'
import { async } from 'regenerator-runtime'

export function dataFormat(time, option) {
  time = +time * 1000
  const d = new Date(time)
  const now = new Date().getTime()

  const diff = (now - d) / 1000
  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    // return parseTime(time, option);
  } else {
    let timeStr = d.getFullYear() + '年' + (d.getMonth() + 1) + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
    return timeStr
  }
}
// 年月日，时分秒
// "YYYY-mm-dd HH:MM"
export function dateFormatL(fmt, date) {
  let ret
  const opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString(), // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  }
  for (let k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'))
    }
  }
  return fmt
}
export function dateFormatT(time) {
  time = +time * 1000
  const d = new Date(time)

  return d.getFullYear() + '/' + (d.getMonth() + parseInt(1)) + '/' + d.getDate()
}

export function trim(str) {
  return String.prototype.trim.call(str)
}

export function isType(arg, type) {
  return Object.prototype.toString.call(arg) === '[object ' + type + ']'
}

export function isWeixin() {
  if (navigator && navigator.userAgent && navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1) {
    return true
  }
  return false
}

export function parseQuery() {
  let res = {}

  // #ifdef H5
  const query = (location.href.split('?')[1] || '').trim().replace(/^(\?|#|&)/, '')

  if (!query) {
    return res
  }

  query.split('&').forEach(param => {
    const parts = param.replace(/\+/g, ' ').split('=')
    const key = decodeURIComponent(parts.shift())
    const val = parts.length > 0 ? decodeURIComponent(parts.join('=')) : null

    if (res[key] === undefined) {
      res[key] = val
    } else if (Array.isArray(res[key])) {
      res[key].push(val)
    } else {
      res[key] = [res[key], val]
    }
  })
  // #endif
  // #ifndef H5
  var pages = getCurrentPages() //获取加载的页面
  var currentPage = pages[pages.length - 1] //获取当前页面的对象
  var url = currentPage.route //当前页面url
  res = currentPage.options //如果要获取url中所带的参数可以查看options
  // #endif

  return res
}

/*获取当前页url*/
export function getCurrentPageUrl() {
  var pages = getCurrentPages() //获取加载的页面
  var currentPage = pages[pages.length - 1] //获取当前页面的对象
  var url = currentPage.route //当前页面url
  return url
}

/*获取当前页带参数的url*/
export function getCurrentPageUrlWithArgs() {
  var pages = getCurrentPages() //获取加载的页面
  var currentPage = pages[pages.length - 1] //获取当前页面的对象
  var url = currentPage.route //当前页面url
  var options = currentPage.options //如果要获取url中所带的参数可以查看options
  //拼接url的参数
  var urlWithArgs = url + '?'
  for (var key in options) {
    var value = options[key]
    urlWithArgs += key + '=' + value + '&'
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
  return urlWithArgs
}

// 复制到剪切板
export const copyClipboard = data => {
  uni.setClipboardData({
    data: data,
    success: res => {
      uni.showToast({
        title: '复制成功',
        icon: 'success',
        duration: 2000,
      })
    },
  })
}

export const getProvider = service => {
  return new Promise((resolve, reject) => {
    // 获取当前环境的服务商
    uni.getProvider({
      service: service || 'oauth',
      success: function(res) {
        // 此处可以排除h5
        if (res.provider) {
          resolve(res.provider[0])
        }
      },
      fail() {
        reject('获取环境服务商失败')
      },
    })
  }).catch(error => {
    console.log('167',error)
  })
}

export const authorize = authorizeStr => {
  return new Promise((resolve, reject) => {
    console.log('检验授权', `scope.${authorizeStr}`)
    uni.getSetting({
      success(res) {
        if (res.authSetting[`scope.${authorizeStr}`]) {
          resolve(true)
        } else {
          resolve(false)
        }
      },
      fail() {
        resolve(false)
      },
    })
  })
}

export const login = loginInfo => {
  return new Promise((resolve, reject) => {
    if (Vue.prototype.$deviceType == 'weixin') {
      // 微信授权登录
      const { code } = parseQuery()
      if (code) {
        auth(code).then(() => {
            let redirect = cookie.get('redirect').replace(/\ /g, '')
            console.log('199',redirect)
            if (redirect) {
              redirect = redirect.split('/pages')[1]
              if (!redirect) {
                redirect = '/Loading/index'
              }
              reLaunch({
                path: '/pages' + redirect,
              })
              cookie.remove('redirect')
            } else {
              reLaunch({
                path: '/pages/home/index',
              })
            }
          }).catch(() => {
            reject('当前运行环境为微信浏览器')
            reLaunch({
              path: '/pages/home/index',
            })
          })
      } else {
      }
      return
    }
    if (Vue.prototype.$deviceType == 'weixinh5') {
      console.log('当前运行环境为H5')
      reject('当前运行环境为H5')
      return
    }
    if (Vue.prototype.$deviceType == 'app') {
      console.log('当前运行环境为app')
      reject('当前运行环境为app')
      return
    }
    getProvider().then(provider => {
        console.log('当前的环境商'+provider)
        if (!provider) {
          reject()
        }
        // 调用登录接口
        uni.login({
          provider: provider,
		  // #ifdef MP-ALIPAY
		   // scopes: 'auth_user',  //alipay need to set empower'type
		   // #endif
          success: async function(loginRes) {
            console.log('uniapp统一登录凭证',loginRes)
            // 微信登录
            let code = loginRes.code
            cookie.set('wxLoginCode', loginRes.code)
            let authorizeRes = await authorize('userInfo')
            if (!wx.getUserProfile && !authorizeRes) {
              reject('用户未授权')
              return
            }
            if (wx.getUserProfile) {
              if (loginInfo) {
                wxappAuth({
                  encryptedData: loginInfo.encryptedData,
                  iv: loginInfo.iv,
                  code: code,
                  spread: cookie.get('spread'),
                }).then(({ data }) => {
                    uni.hideLoading()
                    store.commit('login', data.token, dayjs(data.expires_time))
                    store.dispatch('userInfo', true)
                    getUserInfo().then(user => {
                        console.log('获取用户信息成功'+JSON.stringify(user))
                        uni.setStorageSync('uid', user.data.uid)
                        store.dispatch('setUserInfo', user.data)
                        resolve(user)
                      }).catch(error => {
                        console.log('获取用户信息失败')
                        reject('获取用户信息失败')
                      })
                  }).catch(error => { // 登录接口调用失败
                    console.log(error)
                    reject('登录接口调用失败')
                  })
              } else {
                reject('用户未授权')
                return
              }
            } else {
              uni.getUserInfo({
                provider: provider,
                success: function(user) {
                  console.log('获取用户信息成功1'+JSON.stringify(user))
                  console.log('开始调用登录接口')
                  wxappAuth({
					// phone: user.phone
                    encryptedData: user.encryptedData,
                    iv: user.iv,
                    code: code,
                    spread: cookie.get('spread'),
                  }).then(({ data }) => {
                      console.log('登录成功,开始处理登录信息保存，并获取用户详情')
                      uni.hideLoading()
                      store.commit('login', data.token, dayjs(data.expires_time))
                      store.dispatch('userInfo', true)
                      getUserInfo().then(user => {
                          console.log('获取用户信息成功')
                          uni.setStorageSync('uid', user.data.uid)
                          store.dispatch('setUserInfo', user.data)
                          resolve(user)
                        }).catch(error => {
                          console.log('获取用户信息失败')
                          reject('获取用户信息失败')
                        })
                    }).catch(error => {
                      console.log('登录接口调用失败'+error)
                      reject('登录接口调用失败')
                    })
                },
                fail() {
                  console.log('获取用户信息失败')
                  reject('获取用户信息失败')
                },
              })
            }
          },
          fail() {
            console.log('调用登录接口失败')
            reject('调用登录接口失败')
          },
        })
      })
      .catch(error => {
        reject('获取环境服务商失败')
      })
  })
}

export const handleGetUserInfo = () => {
  getUserInfo().then(res => {
	// #ifdef APP-PLUS
	if(!res.data.appRegisterId){
		const jpushModule = uni.requireNativePlugin('JG-JPush');
		jpushModule.setAlias({//设置别名
			'alias': res.data.username,
			'sequence': 1
		})
	}
	// #endif
    store.dispatch('setUserInfo', res.data);
 //    let redirect = cookie.get('redirect').replace(/\ /g, '')
	// console.log(redirect)
 //    if (redirect) {
 //      reLaunch({
 //        path: redirect,
 //        // query
 //      })
 //      return
 //    }
	
	let urlPath = ''
	let pages = getCurrentPages()
	let currentPage = pages[pages.length - 2]
	let currentRoute = currentPage.$page.fullPath
	console.log(currentRoute)
	// if (!currentRoute) {
	// 	urlPath = '/pages/user/User/index'
	// } else {
	// 	urlPath = currentRoute
	// }
	// uni.reLaunch({
	// 	url: urlPath
	// })
	uni.navigateBack({
		delta: -1
	})
	
    back()
  })
}

export function parseUrl(location) {
  if (typeof location === 'string') return location
  const { path, query } = location
  const queryStr = stringify(query)
  if (!queryStr) {
    return path
  }
  return `${path}?${queryStr}`
}

export function parseRoute($mp) {
  const _$mp = $mp || {}
  const path = _$mp.page && _$mp.page.route
  return {
    path: `/${path}`,
    params: {},
    query: _$mp.query || _$mp.page.options,
    hash: '',
    fullPath: parseUrl({
      path: `/${path}`,
      query: _$mp.query || _$mp.page.options,
    }),
    name: path && path.replace(/\/(\w)/g, ($0, $1) => $1.toUpperCase()),
  }
}

export function handleAuth() {
  /**
   *	如何判断权限?
   *	用户如果登录了系统，会留下两个东西，一个是token，一个是userInfo
   *	token存在会过期的问题，如果长时间没有打开小程序，会导致登录失效，出现打开一个页面瞬间跳转到授权页面的问题
   *		解决办法，保存token的时候加上过期时间，每次请求都取一下缓存里的token
   *	userInfo只是用来限时用户信息，作用并不是很大
   * 	ps：只需要判断 token 是否存在即可
   */
  if (cookie.get('login_status')) {
    return true
  }
  return false
}

export const handleLoginStatus = (location, complete, fail, success) => {
  // 不登录可访问的页面
  let page = [
    {
      path: '/pages/home/index',
      name: '首页',
    },
    {
      path: '/pages/shop/GoodsClass/index',
      name: '商品分类',
    },
    {
      path: '/pages/user/Login/index',
      name: '登录页面',
    },
    {
      path: '/pages/authorization/index',
      name: '授权页面',
    },
  ]

  // 是否可以访问
  let isAuth = false
  let path = ''
  if (typeof location === 'string') {
    path = location
  } else {
    path = location.path
  }

  console.log(path)
  // 判断用户是否有token
  if (!handleAuth()) {
    page.map(item => {
      if (item.path == path) {
        isAuth = true
      }
    })
  } else {
    isAuth = true
  }
  return new Promise((resolve, reject) => {
    resolve({
      url: parseUrl(location),
      complete,
      fail,
      success,
    })
  }).catch(error => {
    console.log(error)
  })
}

export function routerPermissions(url, type) {
	
  let path = url
  if (!path) {
    path = '/' + getCurrentPageUrlWithArgs()
  }
  console.log(Vue.prototype.$deviceType)
  if (Vue.prototype.$deviceType == 'routine') {
    // 如果是微信小程序，跳转到授权页
    // 先校验用户是否授权，如果授权了，进行自动登录
    authorize('userInfo')
      .then(() => {
        login()
          .then(res => {
            // 登录成功，跳转到需要跳转的页面
            store.commit('updateAuthorizationPage', false)
            if (path == '/pages/shop/GoodSearch/apply/index' 
			|| path == '/pages/shop/ShoppingCart/index'
			|| path == '/pagesB/shop/goodsDetails/index' 
			|| path == '/pages/user/User/index' || path == '/pagesA/cloudDeposit/wxIndex') {
              return
            }
            if (type == 'reLaunch') {
              reLaunch({
                path,
              })
              return
            }
            if (type == 'replace') {
              replace({
                path,
              })
              return
            }
            push({
              path,
            })
          })
          .catch(error => {
            console.log('自动登录失败，跳转到授权页面')
            uni.showToast({
              title: error,
              icon: 'none',
              duration: 2000,
            })
            switchTab({
              path: '/pages/authorization/index',
            })
            cookie.set('redirect', path)
          })
      })
      .catch(error => {
        // 跳转到登录页面或者授权页面
        if (path == '/pages/shop/GoodSearch/apply/index' 
		|| path == '/pages/shop/ShoppingCart/index'
		|| path == '/pagesB/shop/goodsDetails/index'
		|| path == '/pages/user/User/index' || path == '/pagesA/cloudDeposit/wxIndex') {
          switchTab({
            path,
          })
          store.commit('updateAuthorizationPage', false)
          return
        }
        reLaunch({
          path: '/pages/authorization/index',
        })
        cookie.set('redirect', path)
      })
  } else if (Vue.prototype.$deviceType == 'weixin') {
    wechat().then(() => oAuth())
    if (!type) {
      push({
        path: url,
      })
    }
  } else {
	// 如果不是小程序跳转到登录页
	cookie.set('redirect', path)
	// #ifdef MP-ALIPAY
	if (path == '/pages/shop/GoodSearch/apply/index' 
	|| path == '/pages/shop/ShoppingCart/index' 
	|| path == '/pagesB/shop/goodsDetails/index'
	|| path == '/pages/user/User/index' || path == '/pagesA/cloudDeposit/wxIndex') {
	  switchTab({
	    path,
	  })
	  store.commit('updateAuthorizationPage', false)
	  return
	}
	reLaunch({
	  path: '/pages/authorization/index',
	})
	// #endif
    // #ifndef MP-ALIPAY
    push({
      path: '/pages/user/Login/index',
    })
	// #endif
  }
}

export function push(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      uni.navigateTo(params)
    })
    .catch(error => {
      // 没有权限
    })
}

export function replace(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      console.log(params)
      uni.redirectTo(params)
    })
    .catch(error => {
      // 没有权限
    })
}

export function reLaunch(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      console.log(params)
      uni.reLaunch(params)
    })
    .catch(error => {
      // 没有权限
      console.log(error)
    })
}

export function go(delta) {
  uni.navigateBack({
    delta,
  })
}

export function back() {
  uni.navigateBack({
    delta: 1,
    success: function(e) {},
    fail: function(e) {},
  })
}

export function switchTab(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      uni.switchTab(params)
    })
    .catch(error => {
      // 没有权限
    })
}

export const _router = {
  mode: 'history',
  switchTab,
  push,
  replace,
  go,
  back,
  reLaunch,
}

export function handleQrCode() {
  var urlSpread = parseQuery()['q']
  if (urlSpread) {
    if (urlSpread.indexOf('%3F') != -1) {
      // 通过海报二维码进来
      urlSpread = urlSpread
        .split('%3F')[1]
        .replace(/%3D/g, ':')
        .replace(/%26/g, ',')
        .split(',')
        .map((item, index) => {
          item = item.split(':')
          return `"${item[0]}":"${item[1]}"`
        })
        .join(',')
      urlSpread = JSON.parse('{' + urlSpread + '}')
      return urlSpread
    } else {
      return handleUrlParam(urlSpread)
    }
  }
  return null
}

export function handleUrlParam(path) {
  var url = path.split('?')[1] //获取url中"?"符后的字串
  var theRequest = new Object()
  if (path.includes('?')) {
    var url = path.split('?')[1] //获取url中"?"符后的字串
    let strs = url.split('&')
    for (var i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
    }
  }
  return theRequest
}

const getImageInfo = images => {
  return new Promise((resolve, reject) => {
    let imageAry = {}
    images.map((item, index) => {
      uni.getImageInfo({
        src: item,
        fail: function(res) {
          imageAry[index] = null
          if (imageAry.length == images.length) {
            resolve(imageAry)
          }
        },
        success: function(res) {
          imageAry[index] = res
          if (Object.keys(imageAry).length == images.length) {
            resolve(imageAry)
          }
        },
      })
    })
  })
}

/**
 * 获取分享海报
 * @param array store 海报素材
 * @param string store_name 素材文字
 * @param string price 价格
 * @param function successFn 回调函数
 *
 *
 */
export const PosterCanvas = (store, successCallBack) => {
  uni.showLoading({
    title: '海报生成中',
    mask: true,
  })
  getImageInfo([store.image, store.code]).then(res => {
    let contentHh = 48 * 1.3
    const ctx = uni.createCanvasContext('myCanvas')
    ctx.clearRect(0, 0, 0, 0)
    const WIDTH = 747
    const HEIGHT = 1326
    ctx.fillStyle = '#FFFFFF'
    ctx.fillRect(0, 0, WIDTH, HEIGHT)
    ctx.drawImage(res[0].path, 0, 0, WIDTH, WIDTH)
    ctx.drawImage(res[1].path, 40, 1064, 200, 200)
    ctx.save()
    let r = 90
    let d = r * 2
    let cx = 40
    let cy = 990
    ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI)
    ctx.clip()
    ctx.restore()
    ctx.setTextAlign('center')
    ctx.setFontSize(48)
    ctx.setFillStyle('#000')
    ctx.fillText(store.title, WIDTH / 2, 810 + contentHh)
    ctx.setTextAlign('center')
    ctx.setFontSize(32)
    ctx.setFillStyle('red')
    ctx.fillText('￥' + store.price, WIDTH / 2, 985)
    ctx.setTextAlign('center')
    ctx.setFontSize(22)
    ctx.setFillStyle('#333333')
    ctx.fillText('长按识别二维码立即购买', WIDTH / 2, 1167)
    ctx.save()
    ctx.draw(true, () => {
      uni.canvasToTempFilePath({
        canvasId: 'myCanvas',
        fileType: 'png',
        destWidth: WIDTH,
        destHeight: HEIGHT,
        success: function(res) {
          uni.hideLoading()
          successCallBack && successCallBack(res.tempFilePath)
        },
        fail: function(error) {
          console.log(error)
        },
      })
    })
  })
}

export const handleLoginFailure = () => {
  console.log('退出登录，标记当前页面为授权页面，防止多次跳转')
  store.commit('logout')
  // 改为授权取消
  store.commit('updateAuthorization', false)
  let currentPageUrl = getCurrentPageUrl()
  if (store.state.$deviceType == 'weixin') {
    // 如果不是授权页面，
    if (!store.getters.isAuthorizationPage) {
      // 标识当前为授权页面
      store.commit('updateAuthorizationPage', true)
      toAuth()
    }
    return
  } else {
    // token 失效
    // 判断当前是不是已经在登录页面或者授权页，防止二次跳转
    if (store.getters.isAuthorizationPage || currentPageUrl == '/pages/user/Login/index') {
      console.log(store.getters.isAuthorizationPage, currentPageUrl, '已经是登录页面或者授权页面，跳出方法')
      return
    }
  }
  store.commit('updateAuthorizationPage', true)
  let path = '/' + getCurrentPageUrlWithArgs()
  let qrCode = handleQrCode()
  routerPermissions(path, 'reLaunch')
}

const handleNoParameters = () => {
  uni.showToast({
    title: '未获取到必要参数，即将跳转首页',
    icon: 'success',
    duration: 2000,
  })
  setTimeout(() => {
    clearTimeout()
    switchTab({
      path: '/pages/home/index',
    })
  }, 1500)
}

export function chooseImage(callback) {
  uni.chooseImage({
    count: 1,
    sourceType: ['album','camera'],
    success: res => {
      uni.getImageInfo({
        src: res.tempFilePaths[0],
        success: image => {
          uni.showLoading({ title: '图片上传中', mask: true })
          uni.uploadFile({
            url: `${VUE_APP_API_URL}/api/upload`,
            file: image,
            filePath: image.path,
			//#ifdef MP-ALIPAY
			fileType:'image',  
			// #endif 
            header: {
              Authorization: 'Bearer ' + store.getters.token,
            },
            name: 'file',
            success: res => {
              if (callback) {
                callback(JSON.parse(res.data).link)
              }
            },
            fail: err => {
              uni.showToast({
                title: '上传图片失败',
                icon: 'none',
                duration: 2000,
              })
            },
            complete: res => {
              uni.hideLoading()
            },
          })
        },
        fail: err => {
          uni.showToast({
            title: '获取图片信息失败',
            icon: 'none',
            duration: 2000,
          })
        },
      })
    },
  })
}
//图片上传
export function syImgUploadFiles(image,path) {
	return new Promise((resolve,reject)=>{
		uni.showLoading({ title: '图片上传中', mask: true })
		uni.uploadFile({
			url: `${VUE_APP_API_URL}/api/upload`,
			file: image,
			filePath: path,
			//#ifdef MP-ALIPAY  
			fileType:'image',  
			// #endif 
			header: {
			  Authorization: 'Bearer ' + store.getters.token,
			},
			name: 'file',
			success: res => {
				resolve(JSON.parse(res.data).link)
			},
			fail: err => {
			  uni.showToast({
				title: '上传图片失败',
				icon: 'none',
				duration: 2000,
			  })
			},
			complete: res => {
			  uni.hideLoading()
			},
		})
	})
}

// 图片压缩递归，小于1M跳出
export function imageCompress(file){
	return new Promise((resolve, reject)=>{
		let { size,path } = file
		let type  = path.split(".")[1]
		//大于1M进行压缩，
		if(size< (1024*1024)){
			resolve(file)
			return false
		}
		uni.compressImage({
			src: path,
			quality: 80,
			success: res => {
				let newPath = res.tempFilePath+type
				let newName = res.tempFilePath.split("/")[res.tempFilePath.split("/").length-1]+type
				uni.getFileInfo({
					filePath:res.tempFilePath,
					success:async (info)=>{
						let newFile = {...file,size:info.size,path:newPath,name:newName,tempFilePath:res.tempFilePath}
						resolve(await imageCompress(newFile))
					}
				})
			}
		})
		
	})
	
}

export function handleErrorMessage(err) {
  console.log(err)
  uni.hideLoading()
  uni.showToast({
    title: err.msg || err.response.data.msg || err.response.data.message || '创建订单失败',
    icon: 'none',
    duration: 2000,
  })
}

//获取当前用户未读&未处理消息
export async function getSyUnhandledMmsgCount(val) {
	let msgCount = 0;
	await getUnhandledMsgCount().then(res => {
		uni.setStorageSync("isRoutineAudit", res.data.isRoutineAudit);
		msgCount = res.data.msgCount;
		val.msgNums = parseInt(msgCount);
		//处理--我的--数字
		let userRole = res.data.userRole;//用户角色：0客户 1客服 2检测师 3客服&检测师
		if(userRole ==1){
			let kfApplyCount = res.data.kfApplyCount;
			val.mgNums = parseInt(kfApplyCount);
		}
		if(userRole ==2){
			let teacherCount = res.data.teacherCount;
			val.mgNums = parseInt(teacherCount);
		}
		if(userRole ==3){
			let sumCount = res.data.sumCount;
			val.mgNums = parseInt(sumCount);
		}
		
		let msgNums = val.msgNums
		let mgNums = val.mgNums
		if (msgNums == 0) {
			// #ifndef MP-ALIPAY
			msgNums = 0
			// #endif
			// #ifdef MP-ALIPAY
			msgNums = ''
			// #endif
		} else if (msgNums > 99) {
			msgNums = '99+'
		} else {
			msgNums = val.msgNums.toString()
		}
		if (mgNums == 0) {
			// #ifndef MP-ALIPAY
			mgNums = 0
			// #endif
			// #ifdef MP-ALIPAY
			mgNums = ''
			// #endif
		} else if (mgNums > 99) {
			mgNums = '99+'
		} else {
			mgNums = val.mgNums.toString()
		}
		
		uni.setTabBarBadge({ //显示数字
		    index: 3, //tabbar下标
		    text: msgNums //数字
		})
		uni.setTabBarBadge({ //显示数字
		    index: 4, //tabbar下标
		    text: mgNums //数字
		})
		
		// #ifdef APP-PLUS
		plus.runtime.setBadgeNumber(parseInt(msgCount + val.mgNums));//应用图标的右上角消除数字，直接传0即可
		// #endif
	}).catch(error => {
		console.log("获取失败")
	})
}

export function removeAppTabBarBadge(val) {
	val.msgNums = 0;
	val.mgNums = 0;
	uni.removeTabBarBadge({ 
	    index: 3, 
	})
	uni.removeTabBarBadge({
	    index: 4,
	})
}

export function numberToCurrencyNo(value) {
  if (!value) return 0
  // 获取整数部分
  const intPart = Math.trunc(value)
  // 整数部分处理，增加,
  const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  // 预定义小数部分
  let floatPart = ''
  // 将数值截取为小数部分和整数部分
  const valueArray = value.toString().split('.')
  if (valueArray.length === 2) { // 有小数部分
    floatPart = valueArray[1].toString() // 取得小数部分
    return intPartFormat + '.' + floatPart
  }
  return intPartFormat + floatPart
}

export function wgs84Togcj02(lng, lat) {
	//GCJ02 转换为 WGS84
	if (out_of_china(lng, lat)) {
	  return [lng, lat]
	}
	const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
	const PI = 3.1415926535897932384626;
	const a = 6378245.0;
	const ee = 0.00669342162296594323;
	let dlat = transformlat(lng - 105.0, lat - 35.0);
	let dlng = transformlng(lng - 105.0, lat - 35.0);
	let radlat = lat / 180.0 * PI;
	let magic = Math.sin(radlat);
	magic = 1 - ee * magic * magic;
	let sqrtmagic = Math.sqrt(magic);
	dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
	dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
	var mglat = lat + dlat;
	var mglng = lng + dlng;
	return [mglng, mglat]
}
export function out_of_china(lng, lat) {
	return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
}
export function transformlat(lng, lat) {
	const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
	const PI = 3.1415926535897932384626;
	const a = 6378245.0;
	const ee = 0.00669342162296594323;
	let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(
	  lng));
	ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
	ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
	ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
	return ret
}
export function transformlng(lng, lat) {
	const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
	const PI = 3.1415926535897932384626;
	const a = 6378245.0;
	const ee = 0.00669342162296594323;
	let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
	ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
	ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
	ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
	return ret
}

export function compareAmounts(amount1, amount2) {
    // 将金额转换为数值类型并去除小数点后多余的0
    var num1 = parseFloat(amount1.replace(/[^\d.-]/g,''));
    var num2 = parseFloat(amount2.replace(/[^\d.-]/g,''));
    if (num1 === num2) {
		return true;
    } else {
        return false;
    }
}